package com.zjsru.plan2024.oneday;

/**
 * 1232. 缀点成线
 *
 * @Author: cookLee
 * @Date: 2024-12-16
 */
public class CheckStraightLine {

    /**
     * 主
     * \
     * 给定一个数组 coordinates ，其中 coordinates[i] = [x, y] ， [x, y] 表示横坐标为 x、纵坐标为 y 的点。请你来判断，这些点是否在该坐标系中属于同一条直线上。
     * \
     * 输入：coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
     * 输出：true
     * \
     * 输入：coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
     * 输出：false
     * \
     *
     * @param args args
     */
    public static void main(String[] args) {
        CheckStraightLine checkStraightLine = new CheckStraightLine();
        int[][] coordinates = new int[][]{{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}};
        System.out.println(checkStraightLine.checkStraightLine(coordinates));
    }

    /**
     * 斜率计算
     *
     * @param coordinates 坐标
     * @return boolean
     */
    public boolean checkStraightLine(int[][] coordinates) {
        int len = coordinates.length;
        boolean result = true;
        int x0 = coordinates[0][0], y0 = coordinates[0][1];
        int x = coordinates[1][0], y = coordinates[1][1];
        for (int i = 2; i < len; i++) {
            //移项斜率计算变为乘法
            if ((coordinates[i][0] - x) * (y - y0) != (coordinates[i][1] - y) * (x - x0)) {
                result = false;
                break;
            }
        }
        return result;
    }

}
